import { CreateModalList } from '@/biz-components/hoc-form/with-list';
import { onFieldInit, onFieldValueChange, onFormMount } from '@formily/core';
import { useRef, useState } from 'react';
import { history } from "umi";
import HistoryModal from '../file-box/components/historyModal';
import api from './services';

const WithListComp = CreateModalList({});

const ArchivesManageList = (props: any) => {
  const formRef = useRef();
  const { match } = props;
  const [visible, setVisible] = useState(false);
  const [viewCode, setViewCode] = useState<any>(null);

  return (
    <>
      <WithListComp
        match={match}
        history={history}
        pageCode={'ArcvManage_L'}
        pageApi={api.page}
        noModal
        noCache
        validator={{
          checkColBtns: (key, row) => {
            if (key === 'bianji') {
              return ![
                "Form",
                "Attachments",
                "Invoices",
                "ElecApvlRec",
                "BankElecReceipt"
              ].includes(row.arcvType);
            }
            if (key === 'chakan') {
              return row.operationAuth?.includes('Details');
            }
            if (key === 'lishi') {
              return row.operationAuth?.includes('HisRecord');
            }

            return true;
          }
        }}
        formConfigs={{
          effects: (form: any) => {
            formRef.current = form;

            // onFormMount(() => {
            //   restFieldsVisible();
            // })
            // onFieldValueChange('type', field => {
            //   restFieldsVisible(field.value);
            // })
            //
            // const restFieldsVisible = (arcvType?: string) => {
            //   form.setFieldState('*', (field: any) => {
            //     if (field.displayName === 'Field') {
            //       field.visible = !field.componentProps.arcvType || (!!arcvType && field.componentProps.arcvType === arcvType);
            //     }
            //   })
            // }
          },
        }}
        events={{
          buttonsEvents: {
            handleCreate: () => {
              history.push('/arcv/archives/archives-manage/create');
            }
          },
          tableButtonsEvents: {
            handleView: (key: any, row: any) => {
              window.open(`/#/arcv/archives/archives-manage/detail/${row.code}/${row.type}/${row.boxCode || undefined}`);
            },
            handleViewHistory: (key: any, row: any) => {
              setVisible(true);
              setViewCode(row.code);
            }
          },
        }}
      />
      <HistoryModal
        title="借阅历史"
        pageApi={api.arcvBrwrPage}
        queryKeys={{
          arcvCode: viewCode,
          pageCode: 'ArcvBrw_L1'
        }}
        visible={visible}
        onCancel={() => {
          setVisible(false);
        }}
      />
    </>
  );
};

export default ArchivesManageList;
